// 侧边栏切换功能
const toggleSidebar = () => {
  document.getElementById('wrapper').classList.toggle('toggled');
};

// 日历功能
const initCalendar = () => {
  const today = new Date();
  const currentMonth = today.getMonth();
  const currentYear = today.getFullYear();
  
  // 生成日历视图
  const generateCalendar = (year, month) => {
    const firstDay = new Date(year, month, 1);
    const lastDay = new Date(year, month + 1, 0);
    const daysInMonth = lastDay.getDate();
    const startingDay = firstDay.getDay();
    
    // 更新日历标题
    const monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    const calendarTitle = document.querySelector('.calendar-title');
    if (calendarTitle) {
      calendarTitle.textContent = `${year}年 ${monthNames[month]}`;
    }
    
    // 生成日历网格
    const calendarGrid = document.querySelector('.calendar-grid');
    if (!calendarGrid) return;
    
    calendarGrid.innerHTML = '';
    
    // 添加空白日期格子
    for (let i = 0; i < startingDay; i++) {
      const emptyDay = document.createElement('div');
      emptyDay.className = 'calendar-day empty';
      calendarGrid.appendChild(emptyDay);
    }
    
    // 添加月份日期
    for (let i = 1; i <= daysInMonth; i++) {
      const dayElement = document.createElement('div');
      dayElement.className = 'calendar-day';
      
      // 标记今天
      if (year === today.getFullYear() && month === today.getMonth() && i === today.getDate()) {
        dayElement.classList.add('today');
      }
      
      const dayNumber = document.createElement('div');
      dayNumber.className = 'day-number';
      dayNumber.textContent = i;
      dayElement.appendChild(dayNumber);
      
      // 这里可以添加事件数据
      // 示例事件
      if (i === 15) {
        const event = document.createElement('div');
        event.className = 'calendar-event';
        event.textContent = '静脉输液技能考核';
        dayElement.appendChild(event);
      }
      
      if (i === 22) {
        const event = document.createElement('div');
        event.className = 'calendar-event';
        event.textContent = '护理文档规范培训';
        dayElement.appendChild(event);
      }
      
      calendarGrid.appendChild(dayElement);
    }
  };
  
  // 初始化日历
  generateCalendar(currentYear, currentMonth);
  
  // 上一月/下一月按钮事件
  const prevMonthBtn = document.querySelector('.prev-month');
  const nextMonthBtn = document.querySelector('.next-month');
  
  let displayMonth = currentMonth;
  let displayYear = currentYear;
  
  if (prevMonthBtn) {
    prevMonthBtn.addEventListener('click', () => {
      displayMonth--;
      if (displayMonth < 0) {
        displayMonth = 11;
        displayYear--;
      }
      generateCalendar(displayYear, displayMonth);
    });
  }
  
  if (nextMonthBtn) {
    nextMonthBtn.addEventListener('click', () => {
      displayMonth++;
      if (displayMonth > 11) {
        displayMonth = 0;
        displayYear++;
      }
      generateCalendar(displayYear, displayMonth);
    });
  }
};

// 初始化页面功能
const initPage = () => {
  // 侧边栏切换按钮
  const sidebarToggle = document.getElementById('sidebarToggle');
  if (sidebarToggle) {
    sidebarToggle.addEventListener('click', toggleSidebar);
  }
  
  // 初始化日历（如果页面有日历元素）
  if (document.querySelector('.calendar-view')) {
    initCalendar();
  }
  
  // 初始化工具提示
  const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
  tooltipTriggerList.forEach(tooltipTriggerEl => {
    new bootstrap.Tooltip(tooltipTriggerEl);
  });
};

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', initPage);